package com.xframe.xdal.core.autofill.proc;

import com.xframe.xdal.core.autofill.IAutoFillBean;
import com.xframe.xdal.core.autofill.IAutoProc;
import com.xframe.xdal.core.util.EncryptUtil;
import com.xframe.xdal.core.util.StringUtil;
import lombok.extern.log4j.Log4j2;

/**
 * @Description: 指定字段加密
 * @ClassName: EncryptProc
 * @Author: Lean.Liu
 * @CreateTime: 2023/3/28 20:59
 **/
@Log4j2
public class EncryptProc implements IAutoProc {
    @Override
    public Object getVal(IAutoFillBean autoFillBean, Object entity, Object val) {
        if (val == null || !(val instanceof String)) {
            return val;
        }
        String args = autoFillBean.getArgs();
        if (StringUtil.isEmpty(args)){
            return val;
        }

        try {
            switch (args.toUpperCase()) {
                case "AES":
                    val = EncryptUtil.encryptAES(String.valueOf(val));
            }
        } catch (Exception e) {
            log.error("加密失败", e.getMessage());
            return val;
        }
        return val;
    }
}
